import http from "../httpService"
import { ShoppingCartInputDto } from "./dto/shoppingCartInputDto"
import { ShoppingCartDto } from "./dto/shoppingCartDto"


class CartService {
    public async createShoppingCartAsync(input :ShoppingCartInputDto) {
        return await http.post('/api/services/app/ShoppingCart/CreateShoppingCartAsync', input)
    }

    public async getShoppingCarts(): Promise<ShoppingCartDto[]>{
        let result = await http.get('/api/services/app/ShoppingCart/GetMyShoppingCarts',{})
        return result.data.result.items
    }

    public async updateShoppingCart(input:ShoppingCartInputDto){
        let result = await http.put('/api/services/app/ShoppingCart/UpdateShoppingCartAsync',input)
        return result.data.result
    }

    public async getMyShoppingCartCount(){
        let result = await http.get('/api/services/app/ShoppingCart/GetMyShoppingCartCount',{})
        return result.data.result
    }


    public async deleteShoppingCarts(ids: number[]) {
        let url_ = '/api/services/app/ShoppingCart/DeleteShoppingCartsAsync?'
        if (ids !== undefined)
            ids && ids.forEach(item => { url_ += 'id=' + encodeURIComponent('' + item) + '&' })
        url_ = url_.replace(/[?&]$/, '')
        let result = await http.delete(url_, {})
        return result.data.result
    }
}

export default new CartService()